local more1__wenji = fk.CreateSkill {
  name = "more1__wenji",
  tags = { Skill.Permanent },
  dynamic_desc = function (self, player)
    if player:getMark("more1__chouti") == 0 then 
      return "more1__wenji"
    else
      return "more1__wenji1"
    end
  end,
}

Fk:loadTranslationTable{
  ["more1__wenji"] = "问计",
  [":more1__wenji"] = "持恒技，你的每个阶段开始时或其他角色回合开始时，若你手牌数为："..--
  "奇数，你可以使用一张手牌："..--
  "偶数，你可以令一名其他角色交给你一张牌。"..--
  "你以此法获得非装备牌时，你记录之。"..--
  "每回合限X次，当你需要使用或打出一张记录的牌时，你将一张牌置于牌堆顶，视为使用之"..--
  "你以此法积累获得二种类型的牌时，本局游戏你使用牌无次数距离限制。（X为全场势力数+1）",--

  ["#more1__wenji-choose"] = "问计：你可以令一名其他角色交给你一张牌",
  ["#more1__wenji-card"] = "问计：你需交给 %src 一张牌",
  ["#more1__wenji-use"] = "问计：你可以使用一张手牌",
  ["#more1__wenji"] = "问计：你可以印一张牌",
  ["@more1__wenji-type"] = "问计类型",
  ["@more1__wenji-turn"] = "问计类型",
  ["@$more1__wenji"] = "问计",
  ["@more1__wenji-round"] = "问计累计",
  ["@more1__wenji-card"] = "问计累计",
  ["@@more1__wenji-inhand"] = "问计",
  [":more1__wenji1"] = "持恒技，其他角色回合开始时或你的判定阶段结束时你可以令一名其他角色交给你一张牌，"..
  "你使用此牌时不计入次数限制，你本回合使用与该牌同类型的牌不能被其他角色响应。",

  ["$more1__wenji1"] = "",
  ["$more1__wenji2"] = "",

}
more1__wenji:addEffect(fk.TurnStart, {
  can_trigger = function(self, event, target, player, data)
    if player:getHandcardNum() % 2 == 0 then 
      return player:hasSkill(more1__wenji.name) and target ~= player and player:getMark("more1__chouti") == 0
      and table.find(player.room:getOtherPlayers(player, false),function(p)return not p:isNude() end)
    else
      return player:hasSkill(more1__wenji.name) and target ~= player and player:getMark("more1__chouti") == 0
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if player:getHandcardNum() % 2 == 0 then 
      local targets = table.filter(room:getOtherPlayers(player, false), function(p)
        return not p:isNude()
      end)
      local to = room:askToChoosePlayers(player, {
        min_num = 1,
        max_num = 1,
        targets = targets,
        skill_name = more1__wenji.name,
        prompt = "#more1__wenji-choose",
        cancelable = true,
      })
      if #to > 0 then
        event:setCostData(self, {tos = to})
        return true
      end
    else
      local use = room:askToPlayCard(player, {
        skill_name = more1__wenji.name,
        prompt = "#more1__wenji-use",
        cancelable = true,
        extra_data = {
          bypass_times = true,
          extraUse = true,
        },
        skip = true,
      })
      if use then
        event:setCostData(self, {extra_data = use})
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if player:getHandcardNum() % 2 == 0 then 
      local to = event:getCostData(self).tos[1]
      local cid = room:askToChooseCard(to, {
        target = to,
        flag = "he",
        skill_name = more1__wenji.name,
      })
      local card = Fk:getCardById(cid)
      if card.type ~= Card.TypeEquip and not table.contains(player:getTableMark("@$more1__wenji"), card.trueName) then
        room:addTableMark(player, "@$more1__wenji", card.trueName)
      end

      if card.type == Card.TypeBasic and player:getMark("more1__wenji_basic") == 0 then--  
        room:addPlayerMark(player, "more1__wenji_basic",1)
        room:addPlayerMark(player, "@more1__wenji-type", 1)
      elseif card.type == Card.TypeTrick and player:getMark("more1__wenji_trick") == 0 then--  
        room:addPlayerMark(player, "more1__wenji_trick",1)
        room:addPlayerMark(player, "@more1__wenji-type", 1)
      elseif card.type == Card.TypeEquip and player:getMark("more1__wenji_equip") == 0 then--  
        room:addPlayerMark(player, "more1__wenji_equip",1)
        room:addPlayerMark(player, "@more1__wenji-type", 1)
      end
      room:obtainCard(player, card, false, fk.ReasonGive, to[1], more1__wenji.name)
      room:addPlayerMark(player,"@more1__wenji-card",1)
    else
      player.room:useCard(event:getCostData(self).extra_data)
    end
  end,
})

more1__wenji:addEffect(fk.EventPhaseStart, {
  can_trigger = function(self, event, target, player, data)
    if player:getHandcardNum() % 2 == 0 then 
      return player:hasSkill(more1__wenji.name) and target == player and player:getMark("more1__chouti") == 0 and player.phase >= Player.Start and player.phase <= Player.Finish
        and table.find(player.room:getOtherPlayers(player, false),function(p)return not p:isNude() end)
    else
      return player:hasSkill(more1__wenji.name) and target == player and player:getMark("more1__chouti") == 0 and player.phase >= Player.Start and player.phase <= Player.Finish
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if player:getHandcardNum() % 2 == 0 then 
      local targets = table.filter(room:getOtherPlayers(player, false), function(p)
        return not p:isNude()
      end)
      local to = room:askToChoosePlayers(player, {
        min_num = 1,
        max_num = 1,
        targets = targets,
        skill_name = more1__wenji.name,
        prompt = "#more1__wenji-choose",
        cancelable = true,
      })
      if #to > 0 then
        event:setCostData(self, {tos = to})
        return true
      end
    else
      local use = room:askToPlayCard(player, {
        skill_name = more1__wenji.name,
        prompt = "#more1__wenji-use",
        cancelable = true,
        extra_data = {
          bypass_times = true,
          extraUse = true,
        },
        skip = true,
      })
      if use then
        event:setCostData(self, {extra_data = use})
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if player:getHandcardNum() % 2 == 0 then 
      local to = event:getCostData(self).tos[1]
      local cid = room:askToChooseCard(to, {
        target = to,
        flag = "he",
        skill_name = more1__wenji.name,
      })
      local card = Fk:getCardById(cid)
      if card.type ~= Card.TypeEquip and not table.contains(player:getTableMark("@$more1__wenji"), card.trueName) then
        room:addTableMark(player, "@$more1__wenji", card.trueName)
      end

      if card.type == Card.TypeBasic and player:getMark("more1__wenji_basic") == 0 then--  
        room:addPlayerMark(player, "more1__wenji_basic",1)
        room:addPlayerMark(player, "@more1__wenji-type", 1)
      elseif card.type == Card.TypeTrick and player:getMark("more1__wenji_trick") == 0 then--  
        room:addPlayerMark(player, "more1__wenji_trick",1)
        room:addPlayerMark(player, "@more1__wenji-type", 1)
      elseif card.type == Card.TypeEquip and player:getMark("more1__wenji_equip") == 0 then--  
        room:addPlayerMark(player, "more1__wenji_equip",1)
        room:addPlayerMark(player, "@more1__wenji-type", 1)
      end
      room:obtainCard(player, card, false, fk.ReasonGive, to[1], more1__wenji.name)
      room:addPlayerMark(player,"@more1__wenji-card",1)
    else
      player.room:useCard(event:getCostData(self).extra_data)
    end
  end,
})

more1__wenji:addEffect("viewas", {
  pattern = ".",
  prompt = "#more1__wenji",
  times = function(self, player)
    local kingdoms = {}
    for _, p in ipairs(Fk:currentRoom().alive_players) do
      table.insertIfNeed(kingdoms, p.kingdom)
    end
    return #kingdoms +1 - player:usedEffectTimes(self.name)
  end,
  interaction = function(self, player)
    local all_names = Fk:getAllCardNames("bt")
    return UI.CardNameBox {
      choices = player:getViewAsCardNames(more1__wenji.name, player:getTableMark("@$more1__wenji"), nil),
      all_choices = all_names,
      default_choice = "AskForCardsChosen",
    }
  end,
  handly_pile = true,
  filter_pattern = {
    min_num = 1,
    max_num = 1,
    pattern = ".",
  },
  view_as = function(self, player, cards)
    if #cards ~= 1 or Fk.all_card_types[self.interaction.data] == nil then return end
    local card = Fk:cloneCard(self.interaction.data)
    card:addSubcard(cards[1])
    card.skillName = more1__wenji.name
    return card
  end,
  before_use = function(self, player, use)
    player.room:removeTableMark(player, "@$more1__wenji", use.card.trueName)
  end,
  after_use = function(self, player, use)
    local room = player.room
    local toPut = use.card:isVirtual() and use.card.subcards or { use.card.id }

    if #toPut > 1 then
      toPut = room:askToGuanxing(player, {
        cards = toPut,
        top_limit = { #toPut, #toPut },
        bottom_limit = { 0, 0 },
        skill_name = more1__wenji.name,
        skip = true,
      }).top
      toPut = table.reverse(toPut)
    end

    room:moveCardTo(toPut, Card.DrawPile, nil, fk.ReasonPut, more1__wenji.name, nil, true)
  end,
  enabled_at_play = function(self, player)
    local kingdoms = {}
    for _, p in ipairs(Fk:currentRoom().alive_players) do
      table.insertIfNeed(kingdoms, p.kingdom)
    end
    return #player:getTableMark("@$more1__wenji") > 0 and player:usedEffectTimes(self.name) < #kingdoms +1
  end,
  enabled_at_response = function(self, player, response)
    local kingdoms = {}
    for _, p in ipairs(Fk:currentRoom().alive_players) do
      table.insertIfNeed(kingdoms, p.kingdom)
    end
    return not response and #player:getTableMark("@$more1__wenji") > 0 and player:usedEffectTimes(self.name) < #kingdoms +1
  end,
})

more1__wenji:addEffect("targetmod", {
  bypass_times = function(self, player, skill, scope, card, to)
    return card and to and player:getMark("@more1__wenji-type") > 1 and player:getMark("more1__chouti") == 0
  end,
  bypass_distances = function(self, player, skill, card, to)
    return card and to and player:getMark("@more1__wenji-type") > 1 and player:getMark("more1__chouti") == 0
  end,
})

more1__wenji:addEffect(fk.TurnStart, {
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(more1__wenji.name) and target ~= player and player:getMark("more1__chouti") > 0 and table.find(player.room:getOtherPlayers(player, false),function(p)return not p:isNude() end)
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = table.filter(room:getOtherPlayers(player, false), function(p)
      return not p:isNude()
    end)
    local to = room:askToChoosePlayers(player, {
      min_num = 1,
      max_num = 1,
      targets = targets,
      skill_name = more1__wenji.name,
      prompt = "#more1__wenji-choose",
      cancelable = true,
    })
    if #to > 0 then
      event:setCostData(self, {tos = to})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos[1]
    local cid = room:askToChooseCard(to, {
      target = to,
      flag = "he",
      skill_name = more1__wenji.name,
    })
    local card = Fk:getCardById(cid)
    room:obtainCard(player, card, false, fk.ReasonGive, to, more1__wenji.name)
    room:setCardMark(card, "@@more1__wenji-inhand", 1) 
    room:addTableMarkIfNeed(player, "@more1__wenji-turn", card:getTypeString().."_char")
    room:addPlayerMark(player,"@more1__wenji-round",1)
  end,
})

more1__wenji:addEffect(fk.EventPhaseEnd, {
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(more1__wenji.name) and target == player and player:getMark("more1__chouti") > 0 and player.phase == Player.Judge and table.find(player.room:getOtherPlayers(player, false),function(p)return not p:isNude() end)
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = table.filter(room:getOtherPlayers(player, false), function(p)
      return not p:isNude()
    end)
    local to = room:askToChoosePlayers(player, {
      min_num = 1,
      max_num = 1,
      targets = targets,
      skill_name = more1__wenji.name,
      prompt = "#more1__wenji-choose",
      cancelable = true,
    })
    if #to > 0 then
      event:setCostData(self, {tos = to})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos[1]
    local cid = room:askToChooseCard(to, {
      target = to,
      flag = "he",
      skill_name = more1__wenji.name,
    })
    local card = Fk:getCardById(cid)
    room:obtainCard(player, card, false, fk.ReasonGive, to, more1__wenji.name)
    room:setCardMark(card, "@@more1__wenji-inhand", 1) 
    room:addTableMarkIfNeed(player, "@more1__wenji-turn", card:getTypeString().."_char")
    room:addPlayerMark(player,"@more1__wenji-round",1)
  end,
})

more1__wenji:addEffect("targetmod", {
  bypass_times = function(self, player, skill, scope, card, to)
    return card and to and card:getMark("@@more1__wenji-inhand") > 1 and player:getMark("more1__chouti") > 0
  end,
})

more1__wenji:addEffect(fk.CardUsing, {
  anim_type = "offensive",
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and table.contains(player:getTableMark("@more1__wenji-turn"), data.card:getTypeString().."_char")
  end,
  on_use = function(self, event, target, player, data)
    data.disresponsiveList = data.disresponsiveList or {}
    for _, p in ipairs(player.room:getOtherPlayers(player, false)) do
      table.insertIfNeed(data.disresponsiveList, p)
    end
  end,
})










return more1__wenji
